/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.SysPerson;
import cn.ibizlab.core.ad.service.SysPersonService;
import cn.ibizlab.core.ad.filter.SysPersonSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysPerson] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysPersonResource {

    @Autowired
    public SysPersonService sysPersonService;

    @Autowired
    @Lazy
    public SysPersonDTOMapping sysPersonDtoMapping;

    @Autowired
    @Lazy
    public SysPersonFilterDTOMapping sysPersonFilterDtoMapping;

    @Autowired
    @Lazy
    public SysPersonInfoDTOMapping sysPersonInfoDtoMapping;

    @Autowired
    @Lazy
    public SysPersonRequestDTOMapping sysPersonRequestDtoMapping;

    @Autowired
    @Lazy
    public SysPersonSimpleInfoDTOMapping sysPersonSimpleInfoDtoMapping;


    /**
    * 创建Create 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"人员" },  notes = "SysPerson-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Create-all') or hasPermission(this.sysPersonDtoMapping.toDomain(#dto),'uaa-SysPerson-Create')")
    @PostMapping(value = {"sys_people","sys_people"})
    public ResponseEntity<SysPersonDTO> create
            (@Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        sysPersonService.create(domain);
        SysPerson rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 人员
    * 
    *
    * @param id id
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"人员" },  notes = "SysPerson-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Get-all') or hasPermission(this.sysPersonDtoMapping.toDomain(returnObject.body),'uaa-SysPerson-Get')")
    @GetMapping("sys_people/{id}")
    public ResponseEntity<SysPersonDTO> getById
            (@PathVariable("id") String id) {
        SysPerson rt = sysPersonService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 人员
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"人员" },  notes = "SysPerson-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Remove-all') or hasPermission(this.sysPersonService.get(#id),'uaa-SysPerson-Remove')")
    @DeleteMapping("sys_people/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysPersonService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 人员
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"人员" },  notes = "SysPerson-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Update-all') or hasPermission(this.sysPersonService.get(#id),'uaa-SysPerson-Update')")
    @VersionCheck(entity = "sysperson" , versionfield = "updateTime")
    @PutMapping("sys_people/{id}")
    public ResponseEntity<SysPersonDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        domain.setId(id);
        sysPersonService.update(domain);
        SysPerson rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * Activate 人员
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "Activate", tags = {"人员" },  notes = "SysPerson-Activate ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Activate-all') or hasPermission(this.sysPersonDtoMapping.toDomain(#dto),'uaa-SysPerson-Activate')")
    @PostMapping("sys_people/{id}/activate")
    public ResponseEntity<SysPersonDTO> activateById
            (@PathVariable("id") String id, @Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        domain.setId(id);
        SysPerson rt = sysPersonService.activate(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * Cache_evict 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "Cache_evict", tags = {"人员" },  notes = "SysPerson-Cache_evict ")
    @PostMapping("sys_people/cache_evict")
    public ResponseEntity<SysPersonDTO> cacheEvict
            (@Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        SysPerson rt = sysPersonService.cacheEvict(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"人员" },  notes = "SysPerson-CheckKey ")
    @PostMapping("sys_people/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        Integer rt = sysPersonService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * Freeze 人员
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "Freeze", tags = {"人员" },  notes = "SysPerson-Freeze ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Freeze-all') or hasPermission(this.sysPersonDtoMapping.toDomain(#dto),'uaa-SysPerson-Freeze')")
    @PostMapping("sys_people/{id}/freeze")
    public ResponseEntity<SysPersonDTO> freezeById
            (@PathVariable("id") String id, @Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        domain.setId(id);
        SysPerson rt = sysPersonService.freeze(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * 草稿GetDraft 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"人员" },  notes = "SysPerson-GetDraft ")
    @GetMapping("sys_people/getdraft")
    public ResponseEntity<SysPersonDTO> getDraft
            (@SpringQueryMap SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        SysPerson rt = sysPersonService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * GetSimple 人员
    * 
    *
    * @param id id
    * @return ResponseEntity<SysPersonSimpleInfoDTO>
    */
    @ApiOperation(value = "GetSimple", tags = {"人员" },  notes = "SysPerson-GetSimple ")
    @GetMapping("sys_people/{id}/getsimple")
    public ResponseEntity<SysPersonSimpleInfoDTO> getSimpleById
            (@PathVariable("id") String id) {
        SysPerson rt = sysPersonService.getSimple(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonSimpleInfoDtoMapping.toDto(rt));
    }

    /**
    * Info 人员
    * 
    *
    * @param id id
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "Info", tags = {"人员" },  notes = "SysPerson-Info ")
    @GetMapping("sys_people/{id}/info")
    public ResponseEntity<SysPersonDTO> infoById
            (@PathVariable("id") String id) {
        SysPerson rt = sysPersonService.info(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * Init 人员
    * 为用户授予基础角色权限
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "Init", tags = {"人员" },  notes = "SysPerson-Init 为用户授予基础角色权限")
    @PostMapping("sys_people/{id}/init")
    public ResponseEntity<SysPersonDTO> initById
            (@PathVariable("id") String id, @Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        domain.setId(id);
        SysPerson rt = sysPersonService.init(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * Items 人员
    * 根据传入的organization_id 查询 organization_id对应单位的人员清单，其余可选过滤参数:department_id/department_id/organizational_role_id
    *
    * @param dto dto
    * @return ResponseEntity<List<SysPersonInfoDTO>>
    */
    @ApiOperation(value = "Items", tags = {"人员" },  notes = "SysPerson-Items 根据传入的organization_id 查询 organization_id对应单位的人员清单，其余可选过滤参数:department_id/department_id/organizational_role_id")
    @PostMapping("sys_people/items")
    public ResponseEntity<List<SysPersonInfoDTO>> items
            (@Validated @RequestBody SysPersonRequestDTO dto) {
        SysPerson domain = sysPersonRequestDtoMapping.toDomain(dto);
        List<SysPerson> rt = sysPersonService.items(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonInfoDtoMapping.toDto(rt));
    }

    /**
    * ResetPassword 人员
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "ResetPassword", tags = {"人员" },  notes = "SysPerson-ResetPassword ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-ResetPassword-all') or hasPermission(this.sysPersonDtoMapping.toDomain(#dto),'uaa-SysPerson-ResetPassword')")
    @PostMapping("sys_people/{id}/resetpassword")
    public ResponseEntity<SysPersonDTO> resetPasswordById
            (@PathVariable("id") String id, @Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        domain.setId(id);
        SysPerson rt = sysPersonService.resetPassword(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * RoleAuth 人员
    * 授权系统角色
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "RoleAuth", tags = {"人员" },  notes = "SysPerson-RoleAuth 授权系统角色")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-RoleAuth-all') or hasPermission(this.sysPersonDtoMapping.toDomain(#dto),'uaa-SysPerson-RoleAuth')")
    @PostMapping("sys_people/{id}/roleauth")
    public ResponseEntity<SysPersonDTO> roleAuthById
            (@PathVariable("id") String id, @Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        domain.setId(id);
        SysPerson rt = sysPersonService.roleAuth(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysPersonDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"人员" },  notes = "SysPerson-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Save-all') or hasPermission(this.sysPersonDtoMapping.toDomain(#dto),'uaa-SysPerson-Save')")
    @PostMapping("sys_people/save")
    public ResponseEntity<SysPersonDTO> save
            (@Validated @RequestBody SysPersonDTO dto) {
        SysPerson domain = sysPersonDtoMapping.toDomain(dto);
        sysPersonService.save(domain);
        SysPerson rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysPersonDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchCur 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysPersonDTO>>
    */
    @ApiOperation(value = "查询FetchCur", tags = {"人员" },  notes = "SysPerson-FetchCur ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-FetchCur-all') or hasPermission(#dto,'uaa-SysPerson-FetchCur')")
    @PostMapping("sys_people/fetchcur")
    public ResponseEntity<List<SysPersonDTO>> fetchCur
            (@Validated @RequestBody SysPersonFilterDTO dto) {
        SysPersonSearchContext context = sysPersonFilterDtoMapping.toDomain(dto);
        Page<SysPerson> domains = sysPersonService.searchCur(context) ;
        List<SysPersonDTO> list = sysPersonDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchCurGroupMember 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysPersonDTO>>
    */
    @ApiOperation(value = "查询FetchCurGroupMember", tags = {"人员" },  notes = "SysPerson-FetchCurGroupMember ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-FetchCurGroupMember-all') or hasPermission(#dto,'uaa-SysPerson-FetchCurGroupMember')")
    @PostMapping("sys_people/fetchcurgroupmember")
    public ResponseEntity<List<SysPersonDTO>> fetchCurGroupMember
            (@Validated @RequestBody SysPersonFilterDTO dto) {
        SysPersonSearchContext context = sysPersonFilterDtoMapping.toDomain(dto);
        Page<SysPerson> domains = sysPersonService.searchCurGroupMember(context) ;
        List<SysPersonDTO> list = sysPersonDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchDefault 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysPersonDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"人员" },  notes = "SysPerson-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-FetchDefault-all') or hasPermission(#dto,'uaa-SysPerson-FetchDefault')")
    @PostMapping("sys_people/fetchdefault")
    public ResponseEntity<List<SysPersonDTO>> fetchDefault
            (@Validated @RequestBody SysPersonFilterDTO dto) {
        SysPersonSearchContext context = sysPersonFilterDtoMapping.toDomain(dto);
        Page<SysPerson> domains = sysPersonService.searchDefault(context) ;
        List<SysPersonDTO> list = sysPersonDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchExternal 人员
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysPersonInfoDTO>>
    */
    @ApiOperation(value = "查询FetchExternal", tags = {"人员" },  notes = "SysPerson-FetchExternal ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-FetchExternal-all') or hasPermission(#dto,'uaa-SysPerson-FetchExternal')")
    @PostMapping("sys_people/fetchexternal")
    public ResponseEntity<List<SysPersonInfoDTO>> fetchExternal
            (@Validated @RequestBody SysPersonFilterDTO dto) {
        SysPersonSearchContext context = sysPersonFilterDtoMapping.toDomain(dto);
        Page<SysPerson> domains = sysPersonService.searchExternal(context) ;
        List<SysPersonInfoDTO> list = sysPersonInfoDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建人员
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Create-all')")
    @ApiOperation(value = "批量新建人员", tags = {"人员" },  notes = "批量新建人员")
	@PostMapping("sys_people/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysPersonDTO> dtos) {
        sysPersonService.createBatch(sysPersonDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除人员
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Remove-all')")
    @ApiOperation(value = "批量删除人员", tags = {"人员" },  notes = "批量删除人员")
	@DeleteMapping("sys_people/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysPersonService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新人员
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Update-all')")
    @ApiOperation(value = "批量更新人员", tags = {"人员" },  notes = "批量更新人员")
	@PutMapping("sys_people/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysPersonDTO> dtos) {
        sysPersonService.updateBatch(sysPersonDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存人员
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Save-all')")
    @ApiOperation(value = "批量保存人员", tags = {"人员" },  notes = "批量保存人员")
	@PostMapping("sys_people/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysPersonDTO> dtos) {
        sysPersonService.saveBatch(sysPersonDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入人员
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysPerson-Save-all')")
    @ApiOperation(value = "批量导入人员", tags = {"人员" },  notes = "批量导入人员")
	@PostMapping("sys_people/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysPersonDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysPersonService.importData(config,ignoreError,sysPersonDtoMapping.toDomain(dtos)));
    }

}
